<?php
   require_once ("../class/mysql_help.class.php");
   require_once("../config/config.php");
   
   class EmployMessage
   {
   	  var $dbHelper;
   	  var $id,$title,$content,$company_id,$date_public,$date_deadline,
   	      $position,$place,$payment,$job_attribute,$industry;
   	      
   	  //构造函数
   	  public function __construct()
      {
      	 $this->dbHelper=new db_Mysql();
      }
      
      //增加信息
      public function Add_employ_message($title,$content,$company_id,$date_public,$date_deadline,
   	                                     $position,$place,$payment,$job_attribute,$industry)
      {
      	 $this->dbHelper->dbconnect();
      	 $cmd=" insert into employ_message(title,content,company_id,date_public,date_deadline,
      	               position,place,payment,job_attribute,industry) values
      	               ('".$title."' ,'".$content."','".$company_id."','".$date_public."','".$date_deadline."','".$position."','".$place."','".$payment."','".$job_attribute."','".$industry."') ";
      	 $result=$this->dbHelper->insert($cmd);
      	 $this->dbHelper->dbclose();
      	 return $result;
      }
      
      //更新信息
      public function employ_message_Update($id,$title,$content,$company_id,$date_public,$date_deadline,
                                           $position,$place,$payment,$job_attribute,$industry)
      {
      	//echo '<script>alert("'.$id.'");</script>';
      	  $this->dbHelper->dbconnect();
		  echo '<script>alert("'.$id.",".$title.",".$content.",".$company_id.",".$date_public.",".$date_deadline.",".
			$position.",".$place,$payment.",".$job_attribute.",".$industry.'")</script>';
      	  $cmd=" update employ_message set title='".$title."',content='".$content."',company_id='".$company_id."',date_public='".$date_public."',date_deadline='".$date_deadline."',
      	          position='".$position."',place='".$place."',payment='".$payment."',job_attribute='".$job_attribute."',industry='".$industry."'  where id='".$id."' ";
      	  $result=$this->dbHelper->update($cmd);
      	  $this->dbHelper->dbclose();
      	  if($result>0) return $id;//更新成功，返回被更新的招聘信息的id
		  else return 0;
      }
  
      //删除信息
      public function employ_message_Delete($Id)
      {
      	 $this->dbHelper->dbconnect();
      	 $cmd=" delete from employ_message where id='".$Id."' ";
      	 $result=$this->dbHelper->delete($cmd);
      	 $this->dbHelper->dbclose();
       	 return $result;
      }
      
      /*功能：查询招聘信息
		@ $id_imployMsg 招聘信息的id
		//返回：$row
		*/	  
      public function employ_message_Select($id_employMsg)
      {
      	 $this->dbHelper->dbconnect();
      	 $cmd="
			SELECT employ_message. * , city.name AS name_city
			FROM employ_message
			LEFT JOIN city ON employ_message.place = city.id
			WHERE employ_message.id = ".$id_employMsg."
			LIMIT 0 , 30 ";
		 $result = $this->dbHelper->execute($cmd);
		 $this->dbHelper->dbclose();
		 $row = $this->dbHelper->fetch_array($result);
		 //var_dump($this->dbHelper);
		 $this->dbHelper->free_result($result);
		 return $row;
		 //$this->dbHelper->free_array($result);
		 //初始化本对象的属性值
		 /*
		 $this->id = $row["id"];
		 $this->title = $row["title"];
		 $this->content = $row["content"];
		 $this->company_id = $row["conpany_id"];
		 $this->date_public = $row["date_public"];
		 $this->date_deadline = $row["date_deadline"];
   	     $this->position = $row["position"];
		 $this->place = $row["place"];
		 $this->payment = $row["payment"];
		 $this->job_attribute = $row["job_attribute"];
		 $this->industry = $row["industry"];
		 */
      }
      
      
      //按id返回改条招聘信息
	  public function GetMessageById($id)
	  {
	  	 $this->dbHelper->dbconnect();
	    // $cmd=" select * from employ_message where id= '".$id."' ";
	  	 //$cmd="select employ_message.* ,city.name as cityName  from employ_message left join city on employ_message.place=city.id  where employ_message.id='".$id."' ";
	  	 $cmd="SELECT employ_message. * , city.name AS cityName
         FROM (
                   SELECT employ_message. * , industry.name AS industryName
                   FROM employ_message
                   LEFT JOIN industry ON employ_message.industry = industry.id
                   WHERE employ_message.id ='".$id."'
              ) AS employ_message
            LEFT JOIN city ON employ_message.place = city.id";
	  	 $result=$this->dbHelper->execute($cmd);
	  	 $row=$this->dbHelper->fetch_array($result);
	  	 $this->id=$row["id"];
	     $this->title=$row["title"];
	     $this->content=$row["content"];
	     $this->date_public=$row["date_public"];
	     $this->date_deadline=$row["date_deadline"];
	     $this->company_id=$row["company_id"];
	     $this->position=$row["position"];
	     $this->place=$row["cityName"];
	     $this->payment=$row["payment"];
	     $this->job_attribute=$row["job_attribute"];
	     $this->industry=$row["industryName"];
	  	 $this->dbHelper->free_result($result);
	  }  
	  /*功能：按公司id返回该公司的所有招聘信息
	  @ $num_page  页码
	  @ $company_id  公司id
	  @ $type_role 访问此函数的人物类型（1代表毕业生，2代表企业）,$type_role暂时不放
	  */
	  public function GetMessageByCId($num_page,$company_id)
	  {
	  	//echo '<script>alert("'.$company_id.'");</script>';
	  	$this->dbHelper->dbconnect();
	  	$pageSize=5;
	  	$num_page=($num_page-1)*$pageSize;
		/*//根据访问此函数的人物的不同，超链接不同
		$addrBy_typeRole = "";
		if($type_role == 1)
		{
			//如果是毕业生用户
		}*/
	  	//echo '<script>alert("'.$num_page.'");</script>';
	  	//$cmd="select employ_message.* ,city.name as cityName  from employ_message left join city on employ_message.place=city.id  where company_id='".$company_id."' ";
        $cmd="SELECT employ_message. * , city.name AS cityName
         FROM (
                  SELECT employ_message. * , industry.name AS industryName
                  FROM employ_message
                  LEFT JOIN industry ON employ_message.industry = industry.id
                  WHERE company_id ='".$company_id."'
              ) AS employ_message
         LEFT JOIN city ON employ_message.place = city.id
		 order by date_public desc
         LIMIT $num_page ,$pageSize ";
	  	$result=$this->dbHelper->execute($cmd);
	  	$str="";
	  	while($row=$this->dbHelper->fetch_array($result))
	  	{
	  		
	  		$job = null;
	  		if((int)$row["job_attribute"]==1) $job ="（全职）";
            else if((int)$row["job_attribute"]==2) $job="（兼职）";
          
            if((int)$row["payment"]==null) $payment="面议";
            else $payment=$row["payment"];
            
	  		$str .='<div class="Recruitment_list_contain">
            	<div class="Recruitment_list_name"><a href="Recruitment_detail.php?id_employMsg='.$row["id"].
					'&companyId='.$company_id.'">'.$row["title"].'</a></div>
                <div class="Recruitment_list_position">职位名称:<span class="fc333">'.
                 $row["position"].$job
              	 .'</span></div>
                <div class="Recruitment_list_location">工作地点：<span class="fc333">'.$row["cityName"].'</span></div>
                <div class="Recruitment_list_endtime">薪资待遇：<span class="fc333">'.$payment.'/月</span></div>
                <div class="Recruitment_list_sep"></div>
				<div class="clear"></div>
            </div>';
              	 
	  	}
	  	
	  	$this->dbHelper->free_result($result);
	  	return  $str;
	  }
	  
      public function Show_pagelist($num_page,$Company_id,$addr_page)
      {
      	$numTotal=mysql_query("SELECT * FROM employ_message where company_id='".$Company_id."' ");
        $num1 = mysql_num_rows($numTotal);
        $num=ceil($num1/5);  //取得总页数，进一取整
        //echo '<script>alert("'.$num.'");</script>';
        if($num < 1)
        {
        	echo "无记录哦亲！";//总页数为0
        	return;
        }
        if($num_page < 1 || $num_page > $num) $num_page = 1;//如果请求的页面不在范围内，现实第一页
        
        //“首页、上一页”
        $str = "";
        if($num_page == 1)	$str .= '<div class="page_box">首页</div> <div class="page_box">上一页</div> ';
        else $str .= '<div class="page_box" onclick="location.href=\''.$addr_page.'1&companyId='.$Company_id.'\';">首页</div> '.
				     '<div class="page_box" onclick="location.href=\''.$addr_page.($num_page - 1).'&companyId='.$Company_id.'\';">上一页</div>';
			
        //中间显示的页码数
        if($num_page < 10)
        {
        	for($i = 1;$i < 12 && $i <= $num;$i++)
        	{
        		if($i == $num_page) $str .= '<div class="page_box on"  onclick="location.href=\''.$addr_page.$i.'&companyId='.$Company_id.'\';">'.$i.'</div>';
        		else $str .= '<div class="page_box"  onclick="location.href=\''.$addr_page.$i.'&companyId='.$Company_id.'\';">'.$i.'</div>';
        	}
        }
        else
        {
        	$num_page_top = $num_page + 5;//显示页数的上限
        	for($i = $num_page - 5;$i < $num_page_top && $i <= $num;$i++)
        	{
        		if($i == $num_page) $str .= '<div class="page_box on"  onclick="location.href=\''.$addr_page.$i.'&companyId='.$Company_id.'\';">'.$i.'</div>';
        		else $str .= '<div class="page_box"  onclick="location.href=\''.$addr_page.$i.'&companyId='.$Company_id.'\';">'.$i.'</div>';
        	}
        }
        
        //“尾页、下一页”
        if($num_page == $num) $str .= '<div class="page_box">下一页</div> <div class="page_box">尾页</div>'.'<div> 共'.$num1.'条</div>';
		else $str .= '<div class="page_box" onclick="location.href=\''.$addr_page.($num_page + 1).'&companyId='.$Company_id.'\';">下一页</div>'.
				'<div class="page_box" onclick="location.href=\''.$addr_page.$num.'&companyId='.$Company_id.'\';">尾页</div> '.
				'<div style=width:500;height:200>  共'.$num1.'条</div>';
		
		//输出
		echo $str;
      }
   }


?>